home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSWRAP.C < prev    next >
C/C++ Source or Header  |  1993-09-02  |  5KB  |  176 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bswrap.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains .
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        MS Windows NT
  33. //        OS/2 2.X+
  34. //        OS/2 2.0 PM
  35. //        SCO UNIX.
  36. //
  37. //    The following compilers are supported:
  38. //        MSC 6.0A
  39. //        MSC/C++ 7.0
  40. //        Borland C++ 3.1 for DOS
  41. //        Borland C++ 1.0 for OS/2 2.X
  42. //        SCO UNIX cc
  43. //
  44. //----------------------------------------------------------------------------
  45. #include <bs.h>
  46.  
  47.  
  48. //----------------------------------------------------------------------------
  49. //   Description:    Determine where to break a string in order to word wrap.
  50. //                          Hard linefeeds are handled. A linefeed will always be
  51. //                        found in the line being displayed. 
  52. //    Parameters:    ppcsz         Pointer to buffer which contains text to wrap.
  53. //                                        This pointer is adjust to reflect the first
  54. //                                        character of the next line.
  55. //                        cSize            Size of line (Does not include \n) to wrap to.
  56. //                        pszOut        Output buffer
  57. //                                        This buffer must be large enough to contain at
  58. //                                        least one line of text:
  59. //                                            SIZE = cSize + (fLf ? 1: 0) + 1
  60. //                                        If null, no text is copied to the buffer
  61. //                        fLf            Append linefeed.
  62. //       Globals:        
  63. //       Returns:    Pointer to first character in output buffer, or NULL.
  64. //----------------------------------------------------------------------------
  65. BOOL FN_E strwrap(PCSZ _FAR_ *ppcsz, SIZET cSize, PSZ pszOut, BOOL fLf)
  66. {
  67.     PCSZ pcsz = *ppcsz;
  68.     SIZET cLen, i;
  69.     PCSZ pcszLf;
  70.  
  71.     Assert(ppcsz);
  72.     Assert(cSize);
  73.  
  74.     cLen = strlen(pcsz);
  75.     if (!cLen)
  76.         return FALSE;
  77.  
  78.     pcszLf = strchr(pcsz, '\n');
  79.    if (pcszLf != NULL)
  80.       {                                 // Found an explicit carriage return 
  81.       SIZET cBuf = (SIZET)(pcszLf - pcsz);       
  82.  
  83.       if (cBuf <= cSize)                    // Check if all text up to and including
  84.          {                                        //  the carriage return will fit
  85.             if (pszOut)
  86.                 {
  87.                 memcpy(pszOut, pcsz, cBuf);
  88.                 pszOut[cBuf] = '\0';            // Terminate buffer
  89.                 }
  90.             pcsz = pcszLf + 1;                // Store pointer to next character
  91.             goto ERROR_EXIT;
  92.          }
  93.       }
  94.    if (cLen <= cSize)                        // Remaining text will fit on one line
  95.       {
  96.         if (pszOut)
  97.             strcpy(pszOut, pcsz);
  98.         pcsz += cLen;                            // Move to next line (null)
  99.         goto ERROR_EXIT;
  100.         }
  101.                                                     // Try to break at space 
  102.    for (i = cSize; i + 1; --i)        
  103.       if (isspace(pcsz[i]))
  104.          {
  105.             if (pszOut)                            // Copy text
  106.                 {
  107.                 if (i)                            
  108.                     memcpy(pszOut, pcsz, i);
  109.                 pszOut[i] = '\0';
  110.                 }
  111.             pcsz += i + 1;
  112.             goto ERROR_EXIT;
  113.          }
  114.     if (pszOut)
  115.         {
  116.         memcpy(pszOut, pcsz, cSize);        // Couldn't find a place to break 
  117.         pszOut[cSize] = '\0';                // Grap a bunch of text, ignore spaces
  118.         }
  119.     pcsz += cSize;
  120.  
  121. ERROR_EXIT:
  122.     *ppcsz = pcsz;                                // Update pointer to next char
  123.     if (fLf && pszOut)                        // Append linefeed
  124.         strcat(pszOut, "\n");
  125.     return TRUE;
  126. }
  127.  
  128.  
  129. //----------------------------------------------------------------------------
  130. //   Description:    Determine the number of lines of text in a buffer which 
  131. //                          has been word wrapped.
  132. //    Parameters:    cSize            Width to wrap to.
  133. //       Globals:    
  134. //       Returns:    Number of lines of text.
  135. //----------------------------------------------------------------------------
  136. SIZET FN_E strwrapcount(PCSZ pcsz, SIZET cSize)
  137. {
  138.     SIZET cLines = 0;
  139.     while (strwrap(&pcsz, cSize, NULL, FALSE))
  140.         cLines++;
  141.  
  142.     return cLines;
  143. }
  144.  
  145.  
  146. //----------------------------------------------------------------------------
  147. //   Description:    Run standard test suite
  148. //    Parameters:    sTest        Test to run.
  149. //                                        0        Run all default tests (except).
  150. //       Returns:    TRUE if successful.
  151. //----------------------------------------------------------------------------
  152. #if COMPILE_TEST
  153. BOOL FN WrapTest(SHORT sTest)
  154. {
  155. static PCSZ pcszWrap = "12345\n\n123 456\n12345 123456\n123\n1";
  156.     CHAR szBuf[20];
  157.     PCSZ pcsz = pcszWrap;
  158.  
  159.     NOTUSED(sTest);
  160.     Output("Number of lines = %d\n", strwrapcount(pcsz, 5));
  161.  
  162.     while (strwrap(&pcsz, 5, szBuf, FALSE))
  163.         Output("[%s]\n", szBuf);
  164.  
  165.     return TRUE;
  166. }
  167. #endif
  168. //----------------------------------------------------------------------------
  169. //------------------------------- End of File --------------------------------
  170. //----------------------------------------------------------------------------
  171.  
  172.  
  173.  
  174.  
  175.  
  176.